// pages/questions/questions.js
const util = require('../../utils/util.js');
const wxPromisify = util.wxPromisify;
const wx_request_promisify = wxPromisify(wx.request);
let prefix = 'https://lxr.tunnel.qydev.com';
var wxCharts = require('../../utils/wxcharts.js');
var pieChart = null;
var app = getApp();
Page({
  data: {
    items: [
      {
        "optionList": [
          {
            "answerCode": "A",
            "answerOrder": 1,
            "answerScore": 5,
            "answerPerson": 10,
            "answerRate": 20,
            "resultOrder": 1,
            "instanceOptionId": "06888903-0331-11e7-b340-507b9dbfbcab",
            "instanceQuestionId": "a71869c2-0330-11e7-b340-507b9dbfbcab",
            "answer": "你要的"
          },
          {
            "answerCode": "B",
            "answerOrder": 3,
            "answerScore": 20,
            "answerPerson": 10,
            "answerRate": 12,
            "resultOrder": 2,
            "instanceOptionId": "0688b9d0-0331-11e7-b340-507b9dbfbcab",
            "instanceQuestionId": "a71869c2-0330-11e7-b340-507b9dbfbcab",
            "answer": "去泰国吧"
          }
        ],
        "questionOrder": 2,
        "questionGroup": "",
        "questionCode": "23",
        "questionName": "册额额我二恶额发到付撒的发",
        "instanceQuestionId": "a71869c2-0330-11e7-b340-507b9dbfbcab",
        "instanceItemId": "4d4509da-0304-11e7-b340-507b9dbfbcab",
        "questionType": 1
      },
      {
        "optionList": [
          {
            "answerCode": "A",
            "answerOrder": 1,
            "answerScore": 10,
            "answerPerson": 9,
            "answerRate": 3,
            "resultOrder": 2,
            "instanceOptionId": "e35a47b2-0330-11e7-b340-507b9dbfbcab",
            "instanceQuestionId": "b1cbbe4b-0330-11e7-b340-507b9dbfbcab",
            "answer": "明天"
          },
          {
            "answerCode": "B",
            "answerOrder": 2,
            "answerScore": 12,
            "answerPerson": 2,
            "answerRate": 1,
            "resultOrder": 3,
            "instanceOptionId": "e35a8570-0330-11e7-b340-507b9dbfbcab",
            "instanceQuestionId": "b1cbbe4b-0330-11e7-b340-507b9dbfbcab",
            "answer": "后天"
          }
        ],
        "questionOrder": 6,
        "questionCode": "飞个飞哥",
        "questionName": "撒的发地方好飞飞哥",
        "instanceQuestionId": "b1cbbe4b-0330-11e7-b340-507b9dbfbcab",
        "instanceItemId": "4d4509da-0304-11e7-b340-507b9dbfbcab",
        "questionType": 1
      },
      {
        "optionList": [
          {
            "answerCode": "B",
            "answerOrder": 3,
            "answerScore": 15,
            "answerPerson": 1,
            "answerRate": 12,
            "resultOrder": 2,
            "instanceOptionId": "f1db3155-0330-11e7-b340-507b9dbfbcab",
            "instanceQuestionId": "ed4ae27d-031c-11e7-b340-507b9dbfbcab",
            "answer": "so easy"
          },
          {
            "answerCode": "A",
            "answerOrder": 2,
            "answerScore": 10,
            "answerPerson": 0,
            "answerRate": 1,
            "resultOrder": 3,
            "instanceOptionId": "f1db0104-0330-11e7-b340-507b9dbfbcab",
            "instanceQuestionId": "ed4ae27d-031c-11e7-b340-507b9dbfbcab",
            "answer": "ggogogo"
          }
        ],
        "questionOrder": 23,
        "questionGroup": "2134234",
        "questionCode": "234",
        "questionName": "沙发地方阿斯蒂芬撒的发舒服的飞阿斯蒂芬飞挨罚挨罚",
        "instanceQuestionId": "ed4ae27d-031c-11e7-b340-507b9dbfbcab",
        "instanceItemId": "4d4509da-0304-11e7-b340-507b9dbfbcab",
        "questionType": 2
      }
    ],
    activeItem: 0,
    btnPre:false,
    btnNext:false,
    seleted:[],//已答题集合
    showPie:true//是否显示饼图
  },
  onLoad: function (options) {
    var _instanceItemId = options.instanceItemId || "";//互动项ID
    this.refeshBtn();
    if (this.data.showPie){
      this.initPie();
    }
    /* 试题列表 */
    // wx_request_promisify({
    //   url: prefix + '/6ds/list.json',
    //   method: 'POST',
    //   data: {
    //     instanceItemId: _instanceItemId
    //   }
    // }).then(res => {
    //   wx.showToast({
    //     title: '成功',
    //     icon: 'success',
    //     duration: 2000
    //   })
    // }).catch(e => {
    //   wx.showModal({
    //     title: '登录失败',
    //     success: function (res) {
    //       if (res.confirm) {

    //       } else if (res.cancel) {

    //       }
    //     }
    //   })
    // })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  initPie:function(){
    //初始化饼图
    pieChart = new wxCharts({
      animation: true,
      canvasId: 'pieCanvas',
      type: 'pie',
      series: [{
        name: '成交量1',
        data: 15,
      }, {
        name: '成交量2',
        data: 35,
      }, {
        name: '成交量3',
        data: 78,
      }, {
        name: '成交量4',
        data: 63,
      }],
      width: 375,
      height: 300,
      dataLabel: true
    });
  },
  optionactive: function (event) {
    //试题点击事件
    var that = this;
    var _data = event.target.dataset;
    var items = this.data.items;
    for (var i = 0; i < items.length; i++) {
      if (_data.instancequestionid == items[i].instanceQuestionId) {
        var _optionList = items[i].optionList;
        for (var j = 0; j < _optionList.length; j++) {
          if (_data.questiontype == 1) {//单选题点击
            if (_data.instanceoptionid == _optionList[j].instanceOptionId && _optionList[j].changeColor==false) {
              _optionList[j].changeColor = true;
            } else {
              _optionList[j].changeColor = false;
            }
          } else if (_data.questiontype == 2) {//多选题
            if (_data.instanceoptionid == _optionList[j].instanceOptionId) {
              _optionList[j].changeColor = _optionList[j].changeColor == false?true:false;
            }
          }
        }
      }
    }
    this.setData({
      items: items
    });
    this.selectedQ();
  },
  refeshBtn:function(){
    // 页面初始化 options为页面跳转所带来的参数
    if (this.data.items.length > 1 && this.data.activeItem < (this.data.items.length-1)) {
      this.setData({
        btnNext: true
      })
    } else {
      this.setData({
        btnNext: false
      })
    }
    if (this.data.activeItem > 0) {
      this.setData({
        btnPre: true
      })
    } else {
      this.setData({
        btnPre: false
      })
    }
  },
  btnNext:function(){
    //下一题
    var nextQ = this.data.activeItem+1;
    this.setData({
      activeItem: nextQ
    })
    this.refeshBtn();
    this.selectedQ();
  },
  btnPre:function(){
    //上一题
    var nextQ = this.data.activeItem - 1;
    this.setData({
      activeItem: nextQ
    })
    this.refeshBtn();
    this.selectedQ();
  },
  selectedQ:function(){
    //已选试题
    var that = this;
    var items = this.data.items;
    var _selected=[];
    for (var i = 0; i < items.length; i++) {
        var _optionList = items[i].optionList;
        var _m=[];//多选题
        for (var j = 0; j < _optionList.length; j++) {
          if (items[i].questionType == 1) {//单选题点击
            if ( _optionList[j].changeColor == true) {
              _selected.push({
                "personAnswer": _optionList[j].answerCode,
                "instanceQuestionId": _optionList[j].instanceQuestionId
              })
            }
          } else if (items[i].questionType == 2) {//多选题
            if (_optionList[j].changeColor == true) {
              _m.push(_optionList[j].answerCode)
            }
          }
        }
        if (items[i].questionType == 2 && _m.length>0) {//多选题
          _selected.push({
            "personAnswer": _m.join(","),
            "instanceQuestionId": items[i].instanceQuestionId
          })
        }
    }
    this.setData({
      seleted: _selected
    })
  },
  btnCommit:function(){
    //提交试卷
    console.log(this.data.seleted)
    this.setData({
      showPie: true
    })
  }
})